package com.bytedance.danmaku.render.engine.demo.demo

import android.graphics.BitmapFactory
import android.graphics.PointF
import android.graphics.RectF
import android.widget.Toast
import com.bytedance.danmaku.render.engine.DanmakuView
import com.bytedance.danmaku.render.engine.control.DanmakuController
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.R
import com.bytedance.danmaku.render.engine.demo.base.BaseDemoActivity
import com.bytedance.danmaku.render.engine.demo.danmu.DanmakuManager
import com.bytedance.danmaku.render.engine.demo.danmu.DanmakuViewImpl
import com.bytedance.danmaku.render.engine.demo.danmu.bordertext.SimpleAdvancedDanmaku
import com.bytedance.danmaku.render.engine.demo.danmu.bordertext.SimpleAdvancedDanmakuDrawItem
import com.bytedance.danmaku.render.engine.demo.danmu.widget.gradienttext.GradientTextData
import com.bytedance.danmaku.render.engine.demo.danmu.widget.gradienttext.GradientTextItemFactory
import com.bytedance.danmaku.render.engine.demo.demo.advanced.AdvancedDanmakuFactory
import com.bytedance.danmaku.render.engine.demo.demo.advanced.DiggData
import com.bytedance.danmaku.render.engine.demo.demo.advanced.DiggDrawItem
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.bitmap.BitmapData
import com.bytedance.danmaku.render.engine.render.draw.text.TextData
import com.bytedance.danmaku.render.engine.touch.IItemClickListener
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_SCROLL


/**
 * Created by dss886 on 2021/04/20.
 */
class SimplestDemoActivity : BaseDemoActivity() {

    //    private lateinit var mController: DanmakuController
    private lateinit var danmakuManager: DanmakuManager
    override fun getLayoutId(): Int {
        return R.layout.activity_simplest_demo
    }

    override fun init() {
        val danmakuView = findViewById<DanmakuView>(R.id.danmaku_view)
//        mController = danmakuView.controller
        danmakuManager = DanmakuManager(DanmakuViewImpl())
        danmakuManager.init(danmakuView)
        danmakuManager.registerDrawItemFactory(DiggDrawItem.DiggDanmakuFactory())
        danmakuManager.registerDrawItemFactory(GradientTextItemFactory())
        danmakuManager.registerDrawItemFactory(SimpleAdvancedDanmakuDrawItem.SimpleAdvancedDanmakuFactory())
        danmakuManager.loadDanmaku(mDanmakuData)
        danmakuManager.setDanmakuSpeed(1f)
        danmakuManager.setDanmakuLineCount(2)
        danmakuManager.setDanmakuTextSize(45f)
        danmakuManager.setDanmakuAlpha(1f)
        danmakuManager.startDanmaku()
        danmakuManager.sendDanmaku(TextData().apply {
            showAtTime=2000
            text = "文字弹幕*********😊☺*****************************************************************************************************"
        })
        danmakuManager.sendBottomDanmakuData(GradientTextData().apply {
            text = "渐变色弹幕"
        })
        danmakuManager.sendDanmaku(SimpleAdvancedDanmaku().apply {
            textData = TextData().apply {
                text = "simple弹幕"
            }
            diggData= DiggData().apply {
                diggIcon = BitmapData().apply {
                    width=30.dp
                    height=30.dp
                    bitmap = BitmapFactory.decodeResource(resources, R.mipmap.test)
                }
                diggText = TextData().apply {
                    text = "simple弹幕"
                }
            }
        })
//        danmakuManager.sendBottomDanmakuData(DiggData().apply {
//            diggIsLeftIcon=true
//            diggIcon = BitmapData().apply {
//                width=30.dp
//                height=30.dp
//                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.test)
//            }
//            diggText = TextData().apply {
//                text = "文字弹幕333333333333333333333333333333333"
//            }
//            layerType = LAYER_TYPE_SCROLL
//        })
        danmakuManager.sendBitmapDanmakuData(100F,100F,resource = BitmapFactory.decodeResource(resources, R.mipmap.test))
        danmakuManager.sendTopDanmakuData(TextData().apply {
            text = "顶部弹幕"
        })
        danmakuManager.sendBottomDanmakuData(TextData().apply {
            text = "底部弹幕"
        })
        danmakuManager.setOnDanmakuClickListener(object : IItemClickListener {
            override fun onDanmakuClick(data: DanmakuData, itemRect: RectF, clickPoint: PointF) {
                Toast.makeText(this@SimplestDemoActivity, "封装后Click: text=${(data as? TextData)?.text}", Toast.LENGTH_SHORT).show()
            }
        })
//        mController.setData(mDanmakuData)
//        mController.start()
    }

    override fun onDestroy() {
        danmakuManager.release()
//        mController.stop()
        super.onDestroy()
    }

}